package cn.lixuan.controller;

import cn.lixuan.constants.BaseInfoProperties;
import cn.lixuan.grace.result.GraceJSONResult;
import cn.lixuan.grace.result.ResponseStatusEnum;
import cn.lixuan.pojo.Users;
import cn.lixuan.service.FansService;
import cn.lixuan.service.UserService;
import cn.lixuan.utils.RedisOperator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Api(tags = "粉丝业务模块接口")
@RequestMapping("fans")
public class FansController {

    @Autowired
    private UserService userService;

    @Autowired
    private FansService fansService;

    @Autowired
    private RedisOperator redis;

    @ApiOperation(value = "关注博主")
    @PostMapping("follow")
    public GraceJSONResult follow(@RequestParam String myId, @RequestParam String vlogerId) {

        // 判断两个id不能为空
        if(StringUtils.isBlank(myId) || StringUtils.isBlank(vlogerId)) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.SYSTEM_ERROR);
        }

        // 判断当前用户自己不能关注自己
        if(myId.equalsIgnoreCase(vlogerId)) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.SYSTEM_ERROR);
        }

        // 判断两个id对应的用户是否存在
        Users myInfo = userService.getUser(myId);
        Users vloger = userService.getUser(vlogerId);

        if(myInfo == null || vloger == null) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.SYSTEM_ERROR);
        }

        fansService.doFollow(myId, vlogerId);

        // 博主的粉丝+1, 我的关注+1, 我和博主的关系， 依赖redis, 不要存入数据库避免db的性能瓶颈
        redis.increment(BaseInfoProperties.REDIS_MY_FOLLOWS_COUNTS + myId, 1);
        redis.increment(BaseInfoProperties.REDIS_MY_FANS_COUNTS + vlogerId, 1);
        redis.set(BaseInfoProperties.REDIS_FANS_AND_VLOGGER_RELATIONSHIP + myId + ":" + vlogerId, "1");



        return GraceJSONResult.ok();
    }


    @ApiOperation(value = "取消关注")
    @PostMapping("cancel")
    public GraceJSONResult cancel(@RequestParam String myId, @RequestParam String vlogerId) {

        fansService.doCancel(myId, vlogerId);

        redis.decrement(BaseInfoProperties.REDIS_MY_FOLLOWS_COUNTS + myId, 1);
        redis.decrement(BaseInfoProperties.REDIS_MY_FANS_COUNTS + vlogerId, 1);
        redis.del(BaseInfoProperties.REDIS_FANS_AND_VLOGGER_RELATIONSHIP + myId + ":" + vlogerId);

        return GraceJSONResult.ok();
    }

    @ApiOperation(value = "查询是否关注该博主")
    @GetMapping("queryDoIFollowVloger")
    public GraceJSONResult queryDoIFollowVloger(@RequestParam String myId, @RequestParam String vlogerId) {
        return GraceJSONResult.ok( fansService.queryDoIFollowVloger(myId, vlogerId));
    }

    @ApiOperation(value = "我的关注列表")
    @GetMapping("queryMyFollows")
    public GraceJSONResult queryMyFollows(@RequestParam String myId, @RequestParam Integer page, @RequestParam Integer pageSize) {
        return GraceJSONResult.ok( fansService.queryMyFollows(myId, page, pageSize));
    }

    @ApiOperation(value = "我的粉丝列表")
    @GetMapping("queryMyFans")
    public GraceJSONResult queryMyFans(@RequestParam String myId, @RequestParam Integer page, @RequestParam Integer pageSize) {
        return GraceJSONResult.ok( fansService.queryMyFans(myId, page, pageSize));
    }
}
